package com.base.cn.platform.os.controller.user.member;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.user.member.CusUserMember;
import com.base.cn.platform.os.service.user.member.CusUserMemberBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Data 用户会员
 * @ClassName  CusUserMemberController
 * @author :ll
 * @Create Date : 2018年4月26日
 */
@RestController
@RequestMapping("/manage/user")
public class CusUserMemberController extends BaseController {

    @Autowired
    private CusUserMemberBiz cusUserMemberBiz;


    /**
     * ajax  搜索列表
     * @return
     */
    @RequestMapping("/queryUserMemberListPage")
    public PageInfo<CusUserMember> queryUserMemberListPage(@RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        return cusUserMemberBiz.queryUserMemberListPage(page,null);
    }

    /**
     * 查询列表
     * @param userMember
     * @return
     */
    @RequestMapping("/queryUserMemberList")
    public List<CusUserMember> queryUserMemberList(@RequestBody CusUserMember userMember){
        return cusUserMemberBiz.queryUserMemberList(userMember);
    }

    /**
     * 根据Id搜索
     * @return
     */
    @RequestMapping("/queryUserMemberById")
    public CusUserMember queryUserMemberById(@RequestParam("id")BigDecimal id){
        return cusUserMemberBiz.findById(id);
    }

    /**
     * 通过ID串查询会员商品列表并转Map
     * @param ids 会员商品列表
     * @return  Map<BigDecimal,CusUserMember>
     */
    @RequestMapping("/findUserMemberToMapByIds")
    public Map<BigDecimal, CusUserMember> findUserMemberToMapByIds(String ids){
        return cusUserMemberBiz.findUserMemberToMapByIds(ids);
    }

    /**
     * 通过ID串，查询会员商品列表
     * @param ids 会员商品ID串
     * @return List<CusUserMember>
     */
    @RequestMapping("/findUserMemberByIds")
    public List<CusUserMember> findUserMemberByIds(@RequestParam("ids") String ids){
        return cusUserMemberBiz.findUserMemberByIds(ids);
    }



    /**
     * 添加修改用户会员
     * @param userMember
     * @return
     */
    @RequestMapping("/saveUpdateUserMember")
    public Map<String,Object> saveUpdateUserMember(@RequestBody CusUserMember userMember){
        String message = "";
        if (DataUtil.idIsNotNull(userMember.getId())){
            message = "修改成功";
        }else{
            message = "添加成功";
        }
        if (StringUtils.isEmpty(userMember.getMemberName())) {
            return ResultUtil.ERROR("请填写会员名");
        }
        if(userMember.getMemberName().length() > 30){
            return  ResultUtil.ERROR("会员名称不能大于30个字符");
        }
        if (userMember.getMemberType()<=0) {
            return ResultUtil.ERROR("请填写会员类型");
        }
        if (userMember.getPrice()<=0) {
            return  ResultUtil.ERROR("请填写会员价格");
        }
        if (userMember.getValidDay()<=0) {
            return ResultUtil.ERROR("请填写会员有效天数");
        }else{
            if(userMember.getValidDay().intValue()<=0){
                return ResultUtil.ERROR("会员有效天数在大于0天");
            }
        }
        if (userMember.getValidDay()>1000) {
            return ResultUtil.ERROR("会员有效天数不能超过1000天");
        }
        if (!StringUtils.isDigit(userMember.getPrice().toString())) {
            return ResultUtil.ERROR("请正确添加会员价格");
        }
        if (StringUtils.isEmpty(userMember.getDepict())) {
            return ResultUtil.ERROR("请填写会员描述");
        }
        if (userMember.getDepict().length() > 30){
            return ResultUtil.ERROR("会员描述不能大于30个字符");
        }
        CusUserMember cusUserMember =cusUserMemberBiz.saveUpdateUserMember(userMember);
        return ResultUtil.SUCCESS(message,cusUserMember);
    }

    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateUserMemberStatus")
    public Map<String,Object> updateUserMemberStatus(@RequestParam("id")BigDecimal id,
                                                     @RequestParam("status")Integer status){
        cusUserMemberBiz.updateUserMemberStatus(id,status);
        String message = "";
        if(status==1){
            message="恢复成功";
        }else if(status==2){
            message="冻结成功";
        }
        return ResultUtil.SUCCESS(message);
    }
}
